package com.wcity.manage.web.property;

import com.alibaba.fastjson.JSON;
import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.Block;
import com.wcity.manage.dao.model.OnDuty;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.BlockService;
import com.wcity.manage.service.property.PropertyOnDutyService;
import com.wcity.manage.service.property.PropertyPayService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 值班信息管理模块
 * Created by zm on 2016/12/14.
 */
@Controller
public class PropertyOnDutyAct {

    @Resource private PropertyOnDutyService propertyOnDutyService;
    @Resource private PropertyPayService propertyPayService;
    @Resource private BlockService blockService;

    /**
     * 获取分页数据
     * @param duty
     * @param pageModel
     * @param request
     * @return
     */
    @RequestMapping("duty_list")
    public String dutyList (OnDuty duty, PageModel pageModel, HttpServletRequest request) {
        try{
            User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
            duty.setUserId(user.getId());
            pageModel.setParams( duty );
            List<OnDuty> list= propertyOnDutyService.getDutyListPage( pageModel );
            for (OnDuty on : list ) {
                Block block = propertyOnDutyService.getUserInfo (on.getBlockId()+"");
                on.setTelephone(block.getTelephone());
            }
            request.setAttribute( "pageModel", pageModel );
            request.setAttribute( "duty", duty );
            request.setAttribute( "userId", user.getId() );
            request.setAttribute( "workNo", user.getWorkNo() );
            request.setAttribute( "list",  list);
        }catch (Exception e){
            e.printStackTrace();
        }

        return "PropertyOnduty/onDutyList";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("to_duty_add")
    public String toDutyList ( HttpServletRequest request ) {
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );

        request.setAttribute( "blockList", propertyPayService.getblockName(user.getId()) );
        return "PropertyOnduty/dutyAdd";
    }

    /**
     * 添加
     * @param duty
     * @param request
     * @param response
     */
    @RequestMapping("duty_add")
    public void dutyList ( OnDuty duty, HttpServletRequest request,HttpServletResponse response) {

        String code = "200";
        String meg = "添加成功";
        Block block = new Block();
        try{
            User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
            duty.setBlockName(propertyOnDutyService.getBlockName ( duty.getBlockId() ).getBlockName());
            block.setId(duty.getBlockId());
            block.setTelephone(duty.getTelephone());
            String errorMsg = blockService.blockEdit( block );
            duty.setUserId(user.getId());
            duty.setCreateTime(new Date());
            int success = propertyOnDutyService.dutyAdd( duty );
            if( success == 0 || !StringUtil.isBlank( errorMsg )){
                code = "300";
                meg = "添加失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "添加失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "duty_list", "", "closeCurrent", ""));
    }

    /**
     * 跳转到修改页面
     * @param request
     * @return
     */
    @RequestMapping("to_duty_edit")
    public String toDutyEdit ( HttpServletRequest request ) {
        try{
            User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
            String id = request.getParameter("id");
            OnDuty duty = propertyOnDutyService.getDutyInfo(id);
            Block block = propertyOnDutyService.getUserInfo (duty.getBlockId()+"");
            request.setAttribute( "telephone", block.getTelephone());
            request.setAttribute( "list", duty);
            request.setAttribute( "blockList", propertyPayService.getblockName(user.getId()) );
        }catch (Exception e){
            e.printStackTrace();
        }
        return "PropertyOnduty/dutyEdit";
    }

    /**
     * 修改
     * @param duty
     */
    @RequestMapping("duty_Edit")
    public void dutyEdit ( OnDuty duty, HttpServletResponse response ) {

        String code = "200";
        String meg = "修改成功";
        Block block = new Block();
        try{
            duty.setBlockName(propertyOnDutyService.getBlockName ( duty.getBlockId() ).getBlockName());
            block.setId(duty.getBlockId());
            block.setTelephone(duty.getTelephone());
            String errorMsg = blockService.blockEdit( block );
            int success = propertyOnDutyService.dutyEdit( duty );
            if( success == 0 || !StringUtil.isBlank( errorMsg )){
                code = "300";
                meg = "修改失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "修改失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "duty_list", "", "closeCurrent", ""));
    }

    /**
     * 删除
     * @param request
     * @param response
     */
    @RequestMapping("duty_delect")
    public void dutyDelect ( HttpServletRequest request ,HttpServletResponse response) {

        String code = "200";
        String meg = "删除成功";
        try{
            String id = request.getParameter("id");
            int success = propertyOnDutyService.dutyDelect( id );
            if( success == 0 ){
                code = "300";
                meg = "删除失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "删除失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "duty_list", "", "", ""));
    }


    /**
     * 获取小区热线
     * @param request
     * @return
     */
    @RequestMapping ("get_phone")
    @ResponseBody
    public Map<String,Object> getPhone (HttpServletRequest request) {
        Map<String,Object> map = new HashMap<>();
        String blockId = request.getParameter("blockId");
        Block block = propertyOnDutyService.getUserInfo (blockId);
        if(block == null){
            map.put("err","小区不存在");
        }else {
            map.put("telePhone",block.getTelephone());
        }
        return map;
    }
}
